package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealDishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult PackagePaginationQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<SetmealVO> setmeals = setmealMapper.PackagePaginationQuery(setmealPageQueryDTO);
        Page<SetmealVO> Page = (Page<SetmealVO>) setmeals;
        PageResult pageResult = PageResult.builder()
                .total(Page.getTotal())
                .records(Page.getResult())
                .build();

        return pageResult;
    }

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "cate_setmeal",allEntries = true)
    public void AddPackage(SetmealDTO setmealDTO) {
        //添加创建字段内容
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setCreateTime(LocalDateTime.now());
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setCreateUser(BaseContext.getCurrentId());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //添加套餐
        setmealMapper.AddPackage(setmeal);
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes!=null && setmealDishes.size()>0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDishMapper.AddPackageDish(setmealId,setmealDish);
            }
        }
    }

    /**
     * 根据ID查询套餐
     * @param id
     * @return
     */
    @Override
    public  SetmealVO idQuery(String id) {
        //查询套餐
        SetmealVO setmealVOS = setmealMapper.idQuery(id);
        Long setmealId = setmealVOS.getId();
        List<SetmealDish> setmealDishes = setmealDishMapper.idQuery(setmealId);
        setmealVOS.setSetmealDishes(setmealDishes);
        return setmealVOS;
    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "cate_setmeal",allEntries = true)
    public void changePackage(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setUpdateTime(LocalDateTime.now());
        setmeal.setUpdateUser(BaseContext.getCurrentId());
        //修改套餐
        setmealMapper.changePackage(setmeal);
        //删除原有套餐菜品
        Long setmealId = setmeal.getId();
        setmealDishMapper.delPackage(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断是否为空,不为空添加套餐菜品
        if(setmealDishes != null && setmealDishes.size()>0){
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDishMapper.changeRelation(setmealId,setmealDish);
            }
        }
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     * @return
     */
    @Override
    @CacheEvict(cacheNames = "cate_setmeal",allEntries = true)
    public String SetmealStateChange(String status, String id) {
        //获取要修改状态套餐里的菜品id
        Integer[] integers = setmealMapper.SetmealStateChangeDishStatus(id);
        //循环判断是否是停售状态
        if (status.equals("1")){
            for (Integer dishId : integers) {
                int DishStatus = dishMapper.findDishStatus(dishId);
                //停售返回
                if (DishStatus == 0)
                    return MessageConstant.SETMEAL_ENABLE_FAILED;
            }
        }
        //没有停售修改状态
        setmealMapper.SetmealStateChange(status,id);
        return null;
    }

    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "cate_setmeal" ,allEntries = true)
    public String DeletSetmeal(Integer[] ids) {
        //循环判断要删除的套餐是否是起售状态
        for (Integer id : ids) {
            int i = setmealMapper.idAllsetmealStatus(id, StatusConstant.ENABLE);
            if (i > 0){
                return MessageConstant.SETMEAL_ON_SALE;
            }
        }
        //删除套餐
        for (Integer id : ids) {
            setmealMapper.DeletSetmeal(id);
            //删除套餐菜品关系
            setmealDishMapper.DeletSetmealDish(id);
        }

        return null;
    }

    /**
     * 根据分类id查询套餐
     * @param categoryId
     * @return
     */
    @Override
    @Cacheable(value = "cate_setmeal", key = "'list'+#categoryId")
    public List<Setmeal> IdSetmeal(String categoryId) {
        Integer shopOpen = StatusConstant.SHOP_OPEN;
        List<Setmeal> setmeals = setmealMapper.IdSetmeal(categoryId, shopOpen);

        return setmeals;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    @Override
    public List<SetmealDishVO> SortFindDish(Long id) {
        List<SetmealDishVO> setmealDishVOS = setmealMapper.SortFindDish(id);
        System.out.println(setmealDishVOS);
        return setmealDishVOS;
    }
}
